﻿using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace CKS.Sandbox.Admin.ContentTypes
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class SetValidationTypeReceiver : SPItemEventReceiver
    {
       /// <summary>
       /// An item is being added.
       /// </summary>
       public override void ItemAdding(SPItemEventProperties properties)
       {
           base.ItemAdding(properties);

           ValidateAndSetType(properties);
       }

       public override void ItemUpdating(SPItemEventProperties properties)
       {
           base.ItemUpdating(properties);

           ValidateAndSetType(properties);
       }

       private static void ValidateAndSetType(SPItemEventProperties properties)
       {
           string strCtype = properties.AfterProperties["ContentType"].ToString();
           if (strCtype == "Public Key Token")
           {
               properties.AfterProperties["CKSValidationType"] = "Public Key Token";

               string hex = properties.AfterProperties["Title"].ToString();
               long output;
               if (hex.Length != 16 ||
                   !long.TryParse(hex, System.Globalization.NumberStyles.HexNumber, null, out output))
               {
                   // it isn't 8 characters
                   // or 
                   // it isn't a valid Hex value
                   properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
                   properties.ErrorMessage = "This is not a valid Public Key Token";
                   properties.RedirectUrl = "/_layouts/CKS.Sandbox.Admin/InvalidPublicKeyToken.aspx";
               }
           }
           else if (strCtype == "Solution ID")
           {
               properties.AfterProperties["CKSValidationType"] = "Solution ID";

               string guid = properties.AfterProperties["Title"].ToString();
               try
               {
                   Guid testGuid = new Guid(guid);
               }
               catch (FormatException)
               {
                   // it isn't a GUID
                   properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
                   properties.ErrorMessage = "This is not a valid Solution ID (GUID)";
                   properties.RedirectUrl = "/_layouts/CKS.Sandbox.Admin/InvalidSolutionID.aspx";
               }
           }
           else if (strCtype == "File Type")
           {
               properties.AfterProperties["CKSValidationType"] = "File Type";
           }
           else if (strCtype == "File RegEx")
           {
               properties.AfterProperties["CKSValidationType"] = "File RegEx";
           }
           else if (strCtype == "File Text String")
           {
               properties.AfterProperties["CKSValidationType"] = "File Text String";
           }
           else if (strCtype == "Assembly Name")
           {
               properties.AfterProperties["CKSValidationType"] = "Assembly Name";
           }
           else if (strCtype == "Assembly Type Namespace")
           {
               properties.AfterProperties["CKSValidationType"] = "Assembly Type Namespace";
           }
       }
    }
}
